package com.ltmonitor.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.dao.ITerminalCommandDao;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.ITerminalCommandService;
import com.ltmonitor.util.DateUtil;

@Service("terminalCommandService")
public class TerminalCommandService extends BaseService<Long, TerminalCommand> implements ITerminalCommandService {

	@Autowired
	private ITerminalCommandDao terminalCommandDao;
	
	
	public List<TerminalCommand> getLatestCommand()
	{
		String hsql = "from TerminalCommand where CreateDate > ? and Status = ? ";
		Date startTime = DateUtil.getDate(DateUtil.now(), Calendar.MINUTE, -5);
		List<TerminalCommand> result = terminalCommandDao.query(hsql,
				new Object[] { startTime, TerminalCommand.STATUS_NEW });
		return result;
	}
	
	/**
	 * 根据流水号找到命令
	 * 
	 * @param sn
	 * @return
	 */
	@Override
	public TerminalCommand getCommandBySn(long sn) {
		String hsql = "from TerminalCommand where SN = ? and createDate > ? order by createDate desc";
		Date startDate = DateUtil.getDate(new Date(), Calendar.HOUR_OF_DAY, -1);
		TerminalCommand tc = (TerminalCommand) this.terminalCommandDao.find(hsql,
				new Object[] { sn, startDate });
		return tc;
	}

	/**
	 * 根据流水号找到命令
	 *
	 * @param sn
	 * @return
	 */
	@Override
	public TerminalCommand getCommandBySn(long sn, int cmdType) {
		String hsql = "from TerminalCommand where SN = ? and createDate > ? and cmdType = ?";
		Date startDate = DateUtil.getDate(new Date(), Calendar.HOUR_OF_DAY, -1);
		TerminalCommand tc = (TerminalCommand) this.terminalCommandDao.find(hsql,
				new Object[] { sn, startDate ,cmdType});
		return tc;
	}
	/**
	 * 根据命令类型找到最近下发的指令命令
	 *
	 */
	@Override
	public TerminalCommand getCommandByCmdType(String simNo, int cmdType) {
		String hsql = "from TerminalCommand where simNo = ? and createDate > ? and cmdType = ? order by createDate desc";
		Date startDate = DateUtil.getDate(new Date(), Calendar.HOUR_OF_DAY, -1);
		TerminalCommand tc = (TerminalCommand) this.terminalCommandDao.find(hsql,
				new Object[] { simNo, startDate ,cmdType});
		return tc;
	}

	

}
